package common;

import java.io.UnsupportedEncodingException;
import java.util.*;
import java.util.logging.*;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author Alejandro
 */
public class ParamStorage {
    private Map<String, String> params;
    private Map<String, String> rows;
    private short sortUid = 0;

    public ParamStorage(HttpServletRequest request) {
        params = new TreeMap<String, String>();
        rows = new TreeMap<String, String>();
        Enumeration names = request.getParameterNames();
        while(names.hasMoreElements()) {
            String key = (String) names.nextElement();
            String value = "";
            try {
                value = new String(request.getParameter(key).trim().getBytes("ISO-8859-1"), "utf-8");
            } catch (UnsupportedEncodingException ex) {
                Logger.getLogger(ParamStorage.class.getName()).log(Level.SEVERE, null, ex);
            }
            
            if (key.startsWith("row_")) {
                String s = key.substring(4);
                rows.put(s, value);
            }

            if (key.startsWith("sort_uid_")) {
                sortUid = Short.parseShort(key.substring(9));
            }
            
            params.put(key, value);
        }
    }

    public Map<String, String> getParamMap() {
        return params;
    }

    public String getParam(String key) {
        return params.get(key);
    }

    public void setParam(String key, String value) {
        if (params.containsKey(key)) {
            params.remove(key);
            params.put(key, value);
        }
    }

    public boolean hasParam(String key) {
        return params.containsKey(key);
    }
    
    public Map<String, String> getRows() {
        return rows;
    }

    public short getSortUid() {
        return sortUid;
    }
}
